<template>
  <div class="app-container">
    <div class="filter-container search_Top">
        <div class="search_input_list">
          <div class="filter-item">
            <el-input clearable  v-model="searchParam.name" placeholder="主题"
                      size="mini"
                      @keyup.enter.native="getList" />
          </div>
        </div>
         <div class="filter-item filter-item-btn-search search_bt_list">
            <span class="gosearch" @click="getList">搜索</span>
            <span class="clears" @click="clearList">清除</span>
         </div>
    </div>
    <div class="table_base_bt">
        <el-button class="filter-btn-item" v-if="hasRole('notice:basedata:add')"
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    @click="handleCreate">新增</el-button>
        <el-button class="filter-btn-item" v-if="hasRole('notice:basedata:del')"
                    size="mini"
                    type="danger"
                    icon="el-icon-delete"
                    @click="batchDelete">删除</el-button>
    </div>
    <drug-table @getSelection="getSelection" :isMultipleSelection="true" :tableData="tableData" :tableLoading="tableLoading" :tableHeader="tableHeader" :option="option">
      <template slot-scope="props" slot="operate">
        <div >
          <label @click="handleEditData(props.rowData)" v-if="hasRole('notice:basedata:edit')"
                 class="table-view" style="cursor: pointer;">
            编辑
          </label>
          <span v-if="hasRole('rule:system:edit')">·</span>
          <label @click="handleDelete(props.rowData)" v-if="hasRole('notice:basedata:del')"
                 class="table-view" style="cursor: pointer;">
            删除
          </label>
        </div>
      </template>
    </drug-table>
    <notice-edit :detailData="detailData" :type="operateType"></notice-edit>
  </div>
</template>

<script>
import waves from '@/views/directive/waves' // waves directive

import drugTable from "@/components/table/index";
import noticeEdit from "./noticeEdit"


export default {
  components: { drugTable,noticeEdit},
  directives: { waves },
  data() {
    return {
      tableData: [],
      tableHeader:[],
      tableLoading:true,
      option:{showOperate:true},
      searchParam: {
        name:"",
        type:"0"
      },
      detailData:{},
      operateType:"add",

      selectChoice:[]
    }
  },
  mounted() {
    let self = this;
    self.getList();
    self.$eventBus.$on("updateNTList",function () {
      self.getList();
    })
  },
  methods: {
    /**
     * 加载消息列表
     */
    getList() {
      let self = this;
      self.tableLoading = true;
      self.$http({
        url: "/drug/notice/queryNoticeList",
        method: "post",
        params:self.searchParam
      }).then(resp => {
        if (resp.success) {
          self.tableLoading = false;
          self.tableData = resp.result;
          self.tableHeader =  [{"columnName": "createTimeFt", "coloumNameCn": "时间","width":"150px"},
            {"columnName": "title", "coloumNameCn": "主题"},
            {"columnName": "publishStatusCn", "coloumNameCn": "状态","width":"100px"}];
          self.option.showOperate = true;
        }
      });
    },
    clearList() {
      let self = this;
      for(let key in self.searchParam){
        self.searchParam[key] = "";
      }
      self.getList()
    },

    handleCreate() {
      this.detailData = {id:"",userName:"",title:"",content:""};
      this.operateType = "add";
    },

    handleEditData(rowData){
      this.detailData = Object.assign({},rowData);
      this.operateType = "edit";
    },

    handleDelete(rowData){
       let param = {ids:[rowData.id]}
       this.deleteRow(param);
    },

    getSelection(val){
      this.selectChoice = val;
    },

    batchDelete(){
      let param = {ids:[]};
      for(let data of this.selectChoice){
        param.ids.push(data.id);
      }
      this.deleteRow(param);
    },

    deleteRow(param) {
      let self = this;
      if(!param || param.ids.length < 1){
        self.$notify({
          title: '提示',
          message: "至少选择一条记录！",
          type: 'warning'
        });
        return
      }
      self.$confirm('确定删除该信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        self.$http({
          url: "/drug/notice/deleteNotice",
          method: "post",
          data: JSON.stringify(param),
          dataType: 'json',
          contentType: "application/json",
        }).then(resp => {
          if (resp.success) {
            self.getList();
            self.$notify({
              title: '提示',
              message: "删除成功！",
              type: 'success'
            });
            return
          }
          self.$notify({
            title: '提示',
            message: "操作失败，请联系管理员！",
            type: 'error'
          });
        });
      });
    }
  }
}
</script>


<style lang="scss" scoped>
.el-form-no {
  text-align: right;
  font-size: 18px;
  color: #cb0000;
}
</style>
